package com.ZYHsheep.dao;

import com.ZYHsheep.entity.BodyMeasure;
import com.ZYHsheep.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class BodyMeasureDao {
    public int insert(BodyMeasure m) {
        String insSql = "INSERT INTO body_measure(Sno,body_height,bust,wide_hips,high_buttocks,weight,body_score) VALUES (?,?,?,?,?,?,?)";
        String updSql = "UPDATE bas_sheep SET body_measure = ?, body_score = ? WHERE Sno = ?";

        try (Connection conn = JDBCUtil.getConn()) {
            conn.setAutoCommit(false);
            try (PreparedStatement ps = conn.prepareStatement(insSql)) {
                ps.setInt(1, m.getSno());
                ps.setObject(2, m.getBodyHeight());
                ps.setObject(3, m.getBust());
                ps.setObject(4, m.getWideHips());
                ps.setObject(5, m.getHighButtocks());
                ps.setObject(6, m.getWeight());
                ps.setObject(7, m.getBodyScore());
                ps.executeUpdate();
            }

            try (PreparedStatement ps = conn.prepareStatement(updSql)) {
                ps.setObject(1, m.getWeight());
                ps.setObject(2, m.getBodyScore());
                ps.setInt(3, m.getSno());
                int rows = ps.executeUpdate();
                if (rows == 0) {
                    conn.rollback();
                    return 0;
                }
            }

            conn.commit();
            return 1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
